package com.yaoli.ajob;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created by will on 2017/7/31.
 */
public class _643_MaximumAverageSubarrayI {
    public double findMaxAverage(int[] nums, int k) {
        Queue<Integer> queue = new LinkedList<Integer>();

        int total = 0;

        for(int i = 0 ; i < k ; i++){
            queue.offer(nums[i]);
            total += nums[i];
        }

        double max = (double)total / (double)k;


        for(int i = k ; i < nums.length ; i++){
            int pre = queue.poll();

            total = total - pre;

            queue.offer(nums[i]);

            total = total + nums[i];

            max = Math.max(max,(double)total / (double) k);
        }

        return max;
    }
}
